package com.xiaobaibai.controller;

import com.alibaba.fastjson.JSON;
import com.xiaobaibai.common.UserCommon;
import com.xiaobaibai.entity.UserInfo;
import com.xiaobaibai.entity.UserIntegralDetail;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.IUserInfoService;
import com.xiaobaibai.service.IUserIntegralDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "积分接口")
@RequestMapping("/integral")
@CrossOrigin
public class IntegralController {

    @Autowired//用户杂七杂八表
    private IUserInfoService userInfoService;

    @Autowired//积分详细
    private IUserIntegralDetailService userIntegralDetailService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${user.checkin}")
    private String checkinJiFen;

    //用户签到接口
    @PostMapping("/userCheckin")
    @ApiOperation("用户签到")
    public ResponseFormat userCheckin(){

        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();

        Integer userId= (Integer) authentication.getPrincipal();

        Long nowDay = getNowDay();
        //1.检验是否已经签到 (如果没有这个key,那么会是false)
        Boolean flag = stringRedisTemplate.opsForSet().isMember(UserCommon.monthCheckin + userId, nowDay.toString());
        if(flag==null||!flag){//为false就去签到
            //可以进行签到,利用Set
            stringRedisTemplate.opsForSet().add(UserCommon.monthCheckin+userId,nowDay.toString());
            //积分记录表更新
            UserIntegralDetail userIntegralDetail=new UserIntegralDetail();
            userIntegralDetail.setUserId(userId);
            userIntegralDetail.setIntegralFlag(true);
            userIntegralDetail.setIntegralContent("每日签到");
            userIntegralDetail.setIntegralQuantity(Integer.parseInt(checkinJiFen));
            userIntegralDetailService.save(userIntegralDetail);
            //增加积分
            userInfoService.addUserInfoIntegral(userId,Integer.parseInt(checkinJiFen));
            return ResponseFormat.ok("签到成功");
        }else{
            //今天已经签到了
            return ResponseFormat.failure("今天已经签到了");
        }

    }

    //获取当前本月当前日
    private Long getNowDay(){
        Calendar instance = Calendar.getInstance();
        return (long) instance.get(Calendar.DATE);
    }

    @GetMapping("/checkin/month")
    @ApiOperation("获取用户当前月份的签到情况")
    public ResponseFormat getThisMonthCheck(){

        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();

        Integer userId= (Integer) authentication.getPrincipal();

        Set<String> set = stringRedisTemplate.opsForSet().members(UserCommon.monthCheckin + userId);

        return ResponseFormat.ok("获取当前月份签到数据成功",set);
    }

    @GetMapping("/detail")
    @ApiOperation("获取用户积分来来往往")
    public ResponseFormat getIntegralDetail(){
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        Integer userId= (Integer) authentication.getPrincipal();
        List<UserIntegralDetail> list = userIntegralDetailService.getUserIntegralDetailByUserId(userId);
        return ResponseFormat.ok("获取用户积分记录成功",list);
    }

}
